package com.voxel.sense.infomsg.module.firm.search;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.example.com.common.app.BaseActivity;
import com.example.com.common.utils.KeyboardUtils;
import com.example.com.common.utils.ToastUtil;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.Factory;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chenyiyao on 2017/11/10.
 */

public class SearchFirmActivity extends BaseActivity {
    @BindView(R.id.et_search)
    EditText et_search; //搜索输入框

    public static void show(Context context) {
        context.startActivity(new Intent(context, SearchFirmActivity.class));
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_search_firm;
    }


    @Override
    protected void initData() {
        super.initData();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        Fragment instantiate = Fragment.instantiate(this, SearchFirmFrag.class.getName());
        fragmentTransaction.replace(R.id.fl_contaent, instantiate);
        fragmentTransaction.commit();
    }

    @Override
    protected void initEven() {
        super.initEven();
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event != null && event.getAction() == KeyEvent.ACTION_DOWN) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_NONE ||
                        actionId == EditorInfo.IME_ACTION_SEARCH ||
                        actionId == EditorInfo.IME_ACTION_GO) {
                    //点击搜索，隐藏键盘
                    KeyboardUtils.hideSoftInput(SearchFirmActivity.this);
                    String trim = v.getText().toString().trim();
                    trrigeSearch(trim);
                }
                return false;
            }
        });
        et_search.setHint(getString(R.string.title_search_hint_firm));
    }

    /**
     * 触发下拉事件
     *
     * @param str
     */
    private void trrigeSearch(String str) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToastLong(Factory.app(), "请输入搜索条件");
        } else {
            //发送通知，进行搜索
            EventBus.getDefault().post(new EventEntity(EventBusConstants.SEARCH_FIRM, str));
        }
    }


    @OnClick({R.id.btn_search_left, R.id.bt_search})
    void onClickBack(View view) {
        if (view.getId() == R.id.btn_search_left) {
            this.onBackPressed();
        } else if (view.getId() == R.id.bt_search) {
            String s = et_search.getText().toString();
            trrigeSearch(s);
        }
    }


}
